package org.iabako.server.controller.timer.demo;

import org.iabako.server.tools.ServerTools;
import org.iabako.shared.entity.business.Address;
import org.iabako.shared.entity.enumeration.Country;
import org.iabako.shared.entity.enumeration.IabakoPackage;
import org.iabako.shared.entity.enumeration.Language;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.EnterpriseBalance;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.entity.user.UserCredit;
import org.iabako.shared.iservice.user.SignUpService;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by jose on 07/01/15.
 */
@Controller
public class CreateUserController {

    @Autowired
    SignUpService signUpService;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Enterprise create(String csvFileEnterprise, Language language) throws IOException, URISyntaxException {

        Enterprise enterprise = getEnterpriseFromCSV(csvFileEnterprise);
        enterprise.setLanguage(language);
        return signUpService.saveEnterprise(enterprise);
    }

    public Enterprise getEnterpriseFromCSV(String csvFileEnterprise) throws URISyntaxException, IOException {

        ServerTools serverTools = new ServerTools();

        URL csvFileURL = this.getClass().getClassLoader().getResource(csvFileEnterprise);
        BufferedReader br = new BufferedReader(new FileReader(new File(csvFileURL.toURI())));
        String line = "";

        int index = 0;

        Enterprise enterprise = null;
        while ((line = br.readLine()) != null) {
            if (index++ == 0) {
                //skip header
                continue;
            }

            String[] token = line.split(";");
            if (token.length == 8) {
                enterprise = new Enterprise();
                enterprise.setDemo(true);
                enterprise.setName(token[0]);
                Address address = new Address();
                address.setAddressLine1(token[1]);
                address.setCity(token[2]);
                address.setCountry(Country.valueOf(token[3]));
                enterprise.setAddress(address);
                enterprise.setPhoneNumber(token[4]);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_MONTH, 1);

                EnterpriseBalance enterpriseBalance = new EnterpriseBalance();
                enterpriseBalance.setStartDate(calendar.getTime());
                Date monthPlusOne = serverTools.addMonthsToDate(calendar.getTime(), 1);
                enterpriseBalance.setEndDate(serverTools.addDaysToDate(monthPlusOne, -1));

                enterprise.setEnterpriseBalance(enterpriseBalance);

                User user = new User();
                user.setFirstName(token[5]);
                user.setLastName(token[6]);
                user.setEmail(token[7]);
                enterprise.setEmail(token[7]);

                //technical fields
                user.setPassword(GenericTools.getMd5("password"));
                user.setActive(true);
                UserCredit userCredit = new UserCredit();
                userCredit.setExpirationDate(new ServerTools().addMonthsToDate(new Date(), 3));
                userCredit.setIabakoPackage(IabakoPackage.proMonthly);

                user.setUserCredit(userCredit);
                user.setEnterprise(enterprise);
                enterprise.getUsers().add(user);
            }
        }
        return enterprise;
    }
}
